package com.trio.gamerental.controller;

import com.trio.gamerental.model.Note;
import com.trio.gamerental.model.User;
import com.trio.gamerental.pageModel.BaseHelper;
import com.trio.gamerental.pageModel.NoteHelper;
import com.trio.gamerental.service.NoteService;
import com.trio.gamerental.util.MD5Util;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 张俊强~ on 2016/10/26.
 * function:
 */
@Controller
@RequestMapping("/note")
public class NoteController {
    @Autowired
    public NoteService noteService;//getAllNotes

    @ResponseBody
    @RequestMapping(value = "/getallnotes",method = RequestMethod.POST)
    public NoteHelper getAllNotes(String page,String row,String userEmail) {
        NoteHelper noteHelper;
        noteHelper=noteService.getAllNotes(page,row,userEmail);

        System.out.println("留言="+noteHelper);
        return noteHelper;
    }

    @RequestMapping(value = "/addnotes",method = RequestMethod.POST)
    public String addNotes(String userEmail,String content) {
        Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=format.format(new Date());

        Integer userId=noteService.getIdByEmail(userEmail);
        if(userId==null) return "";

        Note note=new Note();
        User user=new User();
        user.setUserId(userId);
        note.setUserId(user);
        note.setDate(date);
        note.setNoteContent(content);
        noteService.addNote(note);

        return "/jsp/test/personalNotes.jsp";

    }
}
